package app;

import app.bean.User;
import app.service.UserService;
import app.service.impl.UserServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class App {
    /**存储在线用户**/
    private static final Map<Integer, User> onlineMap = new HashMap<Integer, User>();
    /**键盘输入**/
    private static final Scanner sc = new Scanner(System.in);
    private static int num = 0;
    /**用户业务层**/
    private static final UserService userService = new UserServiceImpl();
    /**当前登录用户**/
    private static User currentUser = new User();
    /**操作用户**/
    private static User user = new User();

    /**
     * 集合的遍历案例 与  数据库测试案例
     **/
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("-------------------信息修改系统登录-------------------\n");
        if (currentUser.getId() == null) {
            login:
            while (true) {
                System.out.println("\t\t\t1，注册");
                System.out.println("\t\t\t2，登录");
                System.out.println("\t\t\t0，结束");
                System.out.print("请选择：");
                int num = sc.nextInt();
                System.out.println("");
                //注册
                if (num == 1) {
                    System.out.println("请输入用户名");
                    user.setUsername(sc.next());
                    System.out.println("请输入密码");
                    user.setPassword(sc.next());
                    boolean res = userService.register(user);
                    if (res) {
                        System.out.println("注册成功!");
                        user = new User();
                    } else {
                        System.err.println("注册失败");
                    }
                }
                //登录
                if (num == 2) {
                    System.out.println("请输入用户名");
                    user.setUsername(sc.next());
                    System.out.println("请输入密码");
                    user.setPassword(sc.next());
                    currentUser = userService.login(user);
                    if (currentUser == null) {
                        System.out.println("用户名不存在或密码错误");
                    } else {
                        System.out.println("登录成功");
                        user = new User();
                        onlineMap.put(currentUser.getId(), currentUser);
                        menu();
                    }
                }
                //退出系统
                if (num == 0) {
                    System.out.println("程序结束！");
                    onlineMap.remove(user.getId());
                    break login;
                }
            }
        }
    }

    public static void menu() {
        index:
        while (true) {
            System.out.println("-------------------欢迎来到信息修改系统-------------------\n");
            System.out.println("\t\t\t1，个人信息");
            System.out.println("\t\t\t2，修改密码");
            System.out.println("\t\t\t3，在线用户");
            if ("ROLE_ADMIN".equals(currentUser.getRole())) {
                System.out.println("\t\t\t4，增加用户");
                System.out.println("\t\t\t5，删除用户");
                System.out.println("\t\t\t6，修改用户");
                System.out.println("\t\t\t7，查询用户");
                System.out.println("\t\t\t8，查询所有用户");
            }
            System.out.println("\t\t\t0，退出登录");
            System.out.print("请选择：");
            num = sc.nextInt();
            System.out.println("");
            user = new User();
            //鉴权
            if (num == 4 || num == 5 || num == 6 || num == 7 || num == 8) {
                if (!"ROLE_ADMIN".equals(currentUser.getRole())) {
                    System.out.println("抱歉你没有权限");
                    continue;
                }
            }
            //个人信息
            if (num == 1) {
                System.out.println("----------------------个人用户信息：----------------------");
                System.out.println("用户ID：" + currentUser.getId() + "\n用户名：" + currentUser.getUsername() + "\n密码：" + currentUser.getPassword() + "\n角色：" + ("ROLE_ADMIN".equals(currentUser.getRole()) ? "超级管理员" : "普通用户"));
            }
            //修改密码
            if (num == 2) {
                user.setId(currentUser.getId());
                System.out.println("请输入旧密码");
                String old = sc.next();
                user.setPassword(old);
                User isExited = userService.queryByNameOrPass(user);
                if (isExited != null) {
                    System.out.println("请输入新密码");
                    user.setPassword(sc.next());
                } else {
                    System.out.println("密码验证错误");
                    continue;
                }
                System.out.println("\n是否修改？（Y/N）");
                if ("y".equals(sc.next())) {
                    boolean res = userService.update(user);
                    if (res) {
                        System.out.println("密码修改成功!!!");
                        user = new User();
                    } else {
                        System.err.println("密码修改失败");
                    }
                } else {
                    System.out.println("\n已取消");
                    continue;
                }
            }
            //在线用户
            if (num == 3) {
                System.out.println("在线用户：");
                onlineMap.forEach((k, v) -> {
                    User us = v;
                    System.out.println(us.getId() + "\t" + us.getUsername() + "\t" + us.getPassword() + "\t" + ("ROLE_ADMIN".equals(us.getRole()) ? "超级管理员" : "普通用户"));
                });
            }
            //增加用户
            if (num == 4) {
                System.out.println("请输入用户名");
                user.setUsername(sc.next());
                System.out.println("请输入密码");
                user.setPassword(sc.next());
                System.out.println("请选择角色：0：管理员/1：普通用户");
                int num = sc.nextInt();
                user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                System.out.println("\n是否添加？（Y/N）");
                if ("y".equals(sc.next())) {
                    boolean res = userService.register(user);
                    if (res) {
                        System.out.println("增加成功!");
                        user = new User();
                    } else {
                        System.err.println("增加失败");
                    }
                } else {
                    System.out.println("\n已取消");
                    continue;
                }
            }
            //删除用户
            if (num == 5) {
                System.out.println("请输入要删除信息的id");
                user.setId(sc.nextInt());
                System.out.println("\n是否删除？（Y/N）");
                if ("y".equals(sc.next())) {
                    boolean res = userService.delete(user.getId());
                    if (res) {
                        System.out.println("删除成功!");
                        user = new User();
                    } else {
                        System.err.println("删除失败");
                    }
                } else {
                    System.out.println("\n已取消");
                    continue;
                }

            }
            //修改用户
            if (num == 6) {
                System.out.println("请输入要修改信息的id");
                user.setId(sc.nextInt());
                user = userService.queryByNameOrPass(user);
                System.out.println("您需要修改的信息如下：");
                System.out.println("用户ID：" + user.getId() + "\n用户名：" + user.getUsername() + "\n密码：" + user.getPassword() + "\n角色：" + ("ROLE_ADMIN".equals(user.getRole()) ? "超级管理员" : "普通用户"));
                System.out.println("\n是否修改？（Y/N）");
                if ("y".equals(sc.next())) {
                    System.out.println("请输入修改的用户名");
                    user.setUsername(sc.next());
                    System.out.println("请输入修改的密码");
                    user.setPassword(sc.next());
                    System.out.println("请选择角色：0：管理员/1：普通用户");
                    int num = sc.nextInt();
                    user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                } else {
                    System.out.println("\n已取消");
                    continue;
                }
                System.out.println("\n是否添加？（Y/N）");
                if ("y".equals(sc.next())) {
                    boolean res = userService.delete(user.getId());
                    if (res) {
                        System.out.println("修改成功!");
                        user = new User();
                    } else {
                        System.err.println("删除失败");
                    }
                } else {
                    System.out.println("\n已取消");
                    continue;
                }
            }
            //条件查询用户
            if (num == 7) {
                System.out.println("请选择查询：（0：单条件查询/1：多条件查询）");
                int num = sc.nextInt();
                if (num == 0) {
                    System.out.println("单条件查询条件：（0：id/1：用户名/2：密码/3：角色）");
                    int a = sc.nextInt();
                    if (a == 0) {
                        System.out.println("请输入要查询的id");
                        user.setId(sc.nextInt());
                    }
                    if (a == 1) {
                        System.out.println("请输入要查询的用户名");
                        user.setUsername(sc.next());
                    }
                    if (a == 2) {
                        System.out.println("请输入要查询的密码");
                        user.setPassword(sc.next());
                    }
                    if (a == 3) {
                        System.out.println("请输入要查询的角色（0/1）");
                        int b = sc.nextInt();
                        user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                    }
                } else {
                    System.out.println("多条件查询条件：（0：（id+用户名）/1：（id+密码）/2：（id+角色）/3：（用户名+密码）/4：（用户名+角色）/5：（密码+角色）/6：全条件）");
                    int c = sc.nextInt();
                    if (c == 0) {
                        System.out.println("请输入要查询的id");
                        user.setId(sc.nextInt());
                        System.out.println("请输入要查询的用户名");
                        user.setUsername(sc.next());
                    }
                    if (c == 1) {
                        System.out.println("请输入要查询的id");
                        user.setId(sc.nextInt());
                        System.out.println("请输入要查询的密码");
                        user.setPassword(sc.next());
                    }
                    if (c == 2) {
                        System.out.println("请输入要查询的id");
                        user.setId(sc.nextInt());
                        System.out.println("请输入要查询的角色（0/1）");
                        int b = sc.nextInt();
                        user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                    }
                    if (c == 3) {
                        System.out.println("请输入要查询的用户名");
                        user.setUsername(sc.next());
                        System.out.println("请输入要查询的密码");
                        user.setPassword(sc.next());
                    }
                    if (c == 4) {
                        System.out.println("请输入要查询的用户名");
                        user.setUsername(sc.next());
                        System.out.println("请输入要查询的角色（0/1）");
                        int b = sc.nextInt();
                        user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                    }
                    if (c == 5) {
                        System.out.println("请输入要查询的密码");
                        user.setPassword(sc.next());
                        System.out.println("请输入要查询的角色（0/1）");
                        int b = sc.nextInt();
                        user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                    }

                    if (c == 6) {
                        System.out.println("请输入要查询的id");
                        user.setId(sc.nextInt());
                        System.out.println("请输入要查询的用户名");
                        user.setUsername(sc.next());
                        System.out.println("请输入要查询的密码");
                        user.setPassword(sc.next());
                        System.out.println("请输入要查询的角色（0/1）");
                        int b = sc.nextInt();
                        user.setRole(num == 0 ? "ROLE_ADMIN" : "ROLE_USER");
                    }
                }
                List<User> res = userService.queryAllByExample(user);
                if (res.size() != 0) {
                    res.forEach(Item -> {
                        System.out.println(Item.getId() + "\t" + Item.getUsername() + "\t" + Item.getPassword() + "\t" + ("ROLE_ADMIN".equals(Item.getRole()) ? "超级管理员" : "普通用户"));
                    });
                } else {
                    System.out.println("\n暂无数据8");
                    continue;
                }

            }
            //查询数据库所有信息
            if (num == 8) {
                List<User> list = userService.queryAllByExample(null);
                System.out.println("\n----------------------用户信息：----------------------\n\n\n");
                System.out.println("用户\t\t用户名\t\t密码\t\t角色\n");
                for (User us : list) {
                    System.out.println(us.getId() + "\t\t" + us.getUsername() + "\t\t" + us.getPassword() + "\t\t" + ("ROLE_ADMIN".equals(us.getRole()) ? "超级管理员" : "普通用户") + "\n");
                }
            }
            //退出登录
            if (num == 0) {
                System.out.println("您已成功退出系统!");
                onlineMap.remove(currentUser.getId());
                break index;
            }
            System.out.println("\n是否继续？（Y/N）");
            if ("y".equals(sc.next())) {
                continue;
            } else {
                System.out.println("您已成功退出系统!");
                break index;
            }
        }
    }
}
